#get package to read in data from stata and other packages
library(foreign)
library(knitr)
library(MSBVAR)
library(magrittr)
library(ggplot2)
library(reshape2)
library(tseries)
library(tseries)
library(gdata)
library(lmtest)
library(car)
library(grid)
library(gridExtra)
require(gridExtra)
library(stargazer)
library(tscount)
library(MASS)
library(strucchange)
library(forecast)
library(ecm)
library(tseries)
library(haven)


#################################################################################################


#################################################################################################
#read in data 
dockettermData <- read_dta("sop2021JLCdata.dta")
dockettermData <- dockettermData[3:69,]
attach(dockettermData)

#make time series
docketterm44 <- ts(dockettermData, start=c(1949), end=c(2015))
docketterm44
docketterm44 [1:10]
is.ts(docketterm44)


#make dataframe so can use better plotting packages
docketterm44.df <- data.frame(docketterm44)
#################################################################################################


#################################################################################################

### APPENDIX FIGURE 5 ###

#vfp plot
vfpplot<-ggplot(data=docketterm44.df, aes(x=term, y=vfpDist)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Veto-Filibuster Pivot") +
  coord_cartesian(ylim=c(0,0.55)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Constraint Distance", breaks=seq(0, 0.5, 0.1)) +
  theme(axis.text=element_text(colour="black",size=20),
        axis.title=element_text(size=20)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=20)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
vfpplot

#chamber median plot
medplot<-ggplot(data=docketterm44.df, aes(x=term, y=medDist)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Chamber Median") +
  coord_cartesian(ylim=c(0,0.55)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Constraint Distance", breaks=seq(0, 0.55, 0.1)) +
  theme(axis.text=element_text(colour="black",size=20),
        axis.title=element_text(size=20)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=20)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
medplot

#jc median plot
jcMedplot<-ggplot(data=docketterm44.df, aes(x=term, y=jcMedDist)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Committee Gatekeeping") +
  coord_cartesian(ylim=c(0,0.55)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Constraint Distance", breaks=seq(0, 0.55, 0.1)) +
  theme(axis.text=element_text(colour="black",size=20),
        axis.title=element_text(size=20)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=20)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
jcMedplot


#maj party median plot
majPlot<-ggplot(data=docketterm44.df, aes(x=term, y=majPartyDist)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Party Gatekeeping") +
  coord_cartesian(ylim=c(0,0.55)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Constraint Distance", breaks=seq(0, 0.55, 0.1)) +
  theme(axis.text=element_text(colour="black",size=20),
        axis.title = element_text(size=20)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=20)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
majPlot


#stack the plots
#must require gridExtra to make this work
appendixPlotConstraint <- grid.arrange(medplot, vfpplot, jcMedplot, majPlot, ncol=2, nrow =2)
ggsave(plot = appendixPlotConstraint, "/Users/elizabethlane/Dropbox/MPSA Revisions/2019 LaTex Files/1. JLC Revisions/LaTexFiles/appendixPlotConstraint.pdf")
#################################################################################################


#################################################################################################

### APPENDIX FIGURE 1 ###

#dv plot
dvPlot<-ggplot(data=docketterm44.df, aes(x=term, y=totalGrantedFJC)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Cases Granted Review") +
  coord_cartesian(ylim=c(50,210)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Cases Granted Review", breaks=seq(50, 210, 10)) +
  theme(axis.text=element_text(colour="black",size=16),
        axis.title = element_text(size=16)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=16)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
dvPlot
ggsave(plot = dvPlot, "/Users/elizabethlane/Dropbox/MPSA Revisions/2019 LaTex Files/1. JLC Revisions/LaTexFiles/dvPlot.pdf")
#################################################################################################


#################################################################################################

### APPENDIX FIGURE 6 ###


#dv plot
laggeddvPlot<-ggplot(data=docketterm44.df, aes(x=term, y=totalGrantedFJC_t1)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Lagged Cases Granted Review") +
  coord_cartesian(ylim=c(50,210)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Cases Granted Review", breaks=seq(50, 210, 10)) +
  theme(axis.text=element_text(colour="black",size=16),
        axis.title = element_text(size=16)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=16)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
laggeddvPlot

totalCertPlot<-ggplot(data=docketterm44.df, aes(x=term, y=totalCertPet)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Total Cert Petitions") +
  coord_cartesian(ylim=c(1000,10500)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Total Cert Petitions", breaks=seq(1000, 10500, 1000)) +
  theme(axis.text=element_text(colour="black",size=16),
        axis.title = element_text(size=16)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=16)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
totalCertPlot


stableCoalitionPlot<-ggplot(data=docketterm44.df, aes(x=term, y=pcthighestcoalition)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Stable Winning Coalition") +
  coord_cartesian(ylim=c(30,80)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Percent of Cases the Stable \n Coalition Signed the Majority Opinion", breaks=seq(30, 80, 10)) +
  theme(axis.text=element_text(colour="black",size=16),
        axis.title = element_text(size=16)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=16)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
stableCoalitionPlot


pctdisconnectedPlot<-ggplot(data=docketterm44.df, aes(x=term, y=pctdisconnected)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Ideologically Disconntected Coalitions") +
  coord_cartesian(ylim=c(5, 45)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Percent of Cases with an \n Ideologically Disconnected Majority", breaks=seq(5, 45, 5)) +
  theme(axis.text=element_text(colour="black",size=16),
        axis.title = element_text(size=16)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=16)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
pctdisconnectedPlot


numCertPoolPlot<-ggplot(data=docketterm44.df, aes(x=term, y=numCertPool)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Cert Pool Participants") +
  coord_cartesian(ylim=c(0,8)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Justices in the Cert Pool", breaks=seq(0, 8, 1)) +
  theme(axis.text=element_text(colour="black",size=16),
        axis.title = element_text(size=16)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=16)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
numCertPoolPlot


absAvgCircSCdistPlot<-ggplot(data=docketterm44.df, aes(x=term, y=absAvgCircSCdist)) +
  geom_line(size=1) +
  theme_bw() +
  theme(text = element_text(size=8)) +
  ggtitle("Average Circuit Distance") +
  coord_cartesian(ylim=c(0,0.7)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  scale_x_continuous(name="Term", breaks=seq(1950, 2015, 5)) +
  scale_y_continuous(name="Absolute Value of the \n Average Circuit Distance", breaks=seq(0, 0.7, 0.1)) +
  theme(axis.text=element_text(colour="black",size=16),
        axis.title = element_text(size=16)) +
  theme(panel.background = element_rect(colour = "black", size=1)) +
  theme(text = element_text(size=16)) +
  theme(panel.grid.major = element_line(colour = "gray80")) +
  theme(plot.title = element_text(face="bold")) +
  theme(panel.grid.minor.x = element_blank()) 
absAvgCircSCdistPlot


#stack the plots
#must require gridExtra to make this work
appendixPlotOtherVars <- grid.arrange(laggeddvPlot, totalCertPlot, stableCoalitionPlot, pctdisconnectedPlot, numCertPoolPlot, absAvgCircSCdistPlot,  ncol=2, nrow =3)
ggsave(plot = appendixPlotOtherVars, "/Users/elizabethlane/Dropbox/MPSA Revisions/2019 LaTex Files/1. JLC Revisions/LaTexFiles/appendixPlotOtherVars.pdf")




